<!--数字类型输入组件-->
<template>
  <a-form-item>
    <template slot="label">
      <span class="c-label-title" :title="label">{{ label }}</span>
      <span v-if="subLabel" class="c-label-sub" :title="subLabel">{{ subLabel }}</span>
    </template>

    <a-input-number
        :style="getStyle"
        :class="{'c-form-disabled':isDisabled,'c-form-input':true}"
        :placeholder="!isDisabled?cplaceholder:''"
        :disabled="isDisabled"
        :step="cstep"
        :max="cmax"
        :min="cmin"
        v-decorator="vDecorator"
        @blur="onBlur"
        @change="change"
    ></a-input-number>
  </a-form-item>
</template>

<script>
import './css/form.less'
import { mixin } from './js/form.js'

export default {
  name: 'AntFormNumber',
  mixins: [mixin],
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: true
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 子标题
     */
    subLabel: {
      type: String,
      default: null
    },
    /**
     * 输入提示
     */
    placeholder: {
      type: String,
      default: ''
    },
    /**
     * 绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * 每次点击增加的值
     */
    step: {
      type: String,
      default: '1'
    },
    /**
     * 最大输入值
     */
    max: {
      type: Number,
      default: Infinity
    },
    /**
     * 最小输入值
     */
    min: {
      type: Number,
      default: -Infinity
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 240
    }
  },
  data() {
    return {
      cstep: this.step,
      cmax: this.max,
      cmin: this.min,
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: this.placeholder ? this.placeholder : '请输入' + this.label
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  created() {
  },
  mounted() {
    // 自动添加验证为空时的message
    let vDecorator = this.vDecorator
    if (vDecorator.length > 1 && vDecorator[1].rules) {
      let rules = vDecorator[1].rules
      rules.forEach(obj => {
        if (obj.required && !obj.message) {
          obj.message = '请选择' + this.label
        }
      })
    }
  },
  watch: {},
  beforeDestroy() {
  },
  methods: {
    /**
     * 选择数据发生改变事件
     * @param value
     * @param data
     */
    change(value, data) {
      this.$nextTick(() => {
        this.$emit('change', value, data)
      })
    },
    /**
     * 失去焦点事件
     * @param event
     */
    onBlur(event) {
      this.$emit('blur', event.target.value)
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .ant-input:hover, ::v-deep .ant-input:focus {
  border-color: @primary-color;
}

::v-deep .ant-form-item-label > label::after {
  content: '';
  display: none;
}
</style>
